<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Xử lý thêm cổ đông</title>
</head>

<body>
<?php
	//include("csdl.php");
	$tencodong=$_POST['tencodong'];
	$daidien=$_POST['daidien'];
	$socophan=$_POST['socophan'];
	$cmnd=$_POST['cmnd'];
	$diachi=$_POST['diachi'];
	$path_thumbs = "upload_images";
			//the new width of the resized image, in pixels.
			$img_thumb_width = 70; // 
			$img_thumb_height=70;
			$extlimit = "yes"; //Limit allowed extensions? (no for all extensions allowed)
			//List of allowed extensions if extlimit = yes
			$limitedext = array(".gif",".jpg",".png",".jpeg",".bmp");
			//echo "<br>i: ".$i."<br>";
			$tam=basename( $_FILES['anh']['name']);
			//echo "fgdk".$tam;
			$tam=explode("%",$tam);
			$tam=implode("",$tam);
			$tam=explode(" ",$tam);
			$tam=implode("",$tam);
			$result='';
			$resize='';
			//echo "<br>".$_FILES['anh'.$i]['name'];
			$duongdan ="upload_images\/".$tam;
			//the image -> variables
			$file_type = $_FILES['anh']['type'];
			$file_name = $_FILES['anh']['name'];
			$file_size = $_FILES['anh']['size'];
			$file_tmp = $_FILES['anh']['tmp_name'];
			$ext = strrchr($file_name,'.');
		   $ext = strtolower($ext);
		   /*if (($extlimit == "yes") && (!in_array($ext,$limitedext))) 
		   {
			  echo "Wrong file extension.  <br>--<a href=\"$_SERVER[PHP_SELF]\">back</a>";
			  exit();
		   }*/
		   //so, whats the file's extension?
		   $getExt = explode ('.', $file_name);
		   $file_ext = $getExt[count($getExt)-1];
		   $rand_name = md5(time());
		   $rand_name= rand(0,999999999);
		   $ThumbWidth = $img_thumb_width;
		    $ThumbHeight = $img_thumb_height;
		   if($file_size)
		   {
			  if($file_type == "image/pjpeg" || $file_type == "image/jpeg")
			  {
				   $new_img = imagecreatefromjpeg($file_tmp);
			   }
			   elseif($file_type == "image/x-png" || $file_type == "image/png")
			   {
				   $new_img = imagecreatefrompng($file_tmp);
			   }
			   elseif($file_type == "image/gif")
			   {
				   $new_img = imagecreatefromgif($file_tmp);
			   }
			   //list the width and height and keep the height ratio.
			   list($width, $height) = getimagesize($file_tmp);
			   $newwidth = $ThumbWidth;
			   $newheight = $ThumbHeight;
			   //calculate the image ratio
			   /*$imgratio=$width/$height;
			   if ($imgratio>1)
			   {
				  $newwidth = $ThumbWidth;
				  //$newheight = $ThumbWidth/$imgratio;
				  $newheight = $ThumbHeight;
			   }
			   else
			   {
					 $newheight = $ThumbWidth;
					 $newwidth = $ThumbWidth*$imgratio;
			   }*/
			   //function for resize image.
			   if (function_exists(imagecreatetruecolor))
			   {
			   $resized_img = imagecreatetruecolor($newwidth,$newheight);
			   }
			   else
			   {
					 die("Error: Please make sure you have GD library ver 2+");
			   }
			   //the resizing is going on here!
			   imagecopyresized($resized_img, $new_img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
			   //finally, save the image
			   ImageJpeg ($resized_img,"$path_thumbs/$rand_name.$file_ext");
			   ImageDestroy ($resized_img);
			   ImageDestroy ($new_img);
			}
			if(@move_uploaded_file($_FILES['anh']['tmp_name'], $duongdan)) 
		   {
		   		@move_uploaded_file ($file_tmp, "$path_thumbs/$rand_name.$file_ext");
			  $result = $duongdan;
			  $resize="$path_thumbs/$rand_name.$file_ext";
			  $knoi->queryCsdl("insert into hinhanh values('','$result','$resize','','$matt')");
		   }
	if(isset($_REQUEST['stt']))
	{
		$stt=$_REQUEST['stt'];
		if($resize!='')
			$query="update codong set tencodong='$tencodong',nguoidaidien='$daidien',socophanphothong='$socophan',cmnd='$cmnd',logo='$resize',diachi='$diachi' where stt='$stt'";
		else
			$query="update codong set tencodong='$tencodong',nguoidaidien='$daidien',socophanphothong='$socophan',cmnd='$cmnd',diachi='$diachi' where stt='$stt'";
		if($knoi->queryCsdl($query))
		{
		?>
		<script language="javascript">
			alert("Đã cập nhật thông tin cổ đông thành công!");
		</script>
		<?php
		//echo "sdfjkf: ".$result;
		echo "<meta http-equiv='refresh' content='0;url=home_admin.php?module=thongtincodong&stt=$stt' />";
		}
		else
		{
		?>
			<script language="javascript">
				alert("Có lỗi xãy ra. Xin vui lòng kiểm tra lại cxvcx!");
			</script>
		<?php
			echo "<meta http-equiv='refresh' content='0;url=home_admin.php?module=thongtincodong&stt=$stt&tencodong=$tencodong&daidien=$daidien&socophan=$socophan&diachi=$diachi&cmnd=$cmnd' />";
		}
	}
	else
	{
		$layscd=$knoi->queryCsdl("select max(stt) as max_stt from codong");
		$scd=mysql_fetch_array($layscd);
		$max_cd=$scd['max_stt'];
		if($max_cd=="")
			$stt=1;
		else 
			$stt=$max_cd + 1;
		if($knoi->queryCsdl("insert into codong values('$stt','$tencodong','$daidien','$socophan','$cmnd','$resize','$diachi','01')"))
		{
		?>
			<script language="javascript">
				alert("Đã thêm cổ đông thành công!");
			</script>
		<?php
			echo "<meta http-equiv='refresh' content='0;url=home_admin.php?module=danhsachcodong' />";
		}
		else
		{
		?>
			<script language="javascript">
				alert("Có lỗi xãy ra. Xin vui lòng kiểm tra lại!");
			</script>
		<?php
			echo "<meta http-equiv='refresh' content='0;url=history.go(-1)' />";
		}
	}
?>
</body>
</html>
